/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Compras;

import Controlador.Bean.Almacen.Producto;
import Controlador.Bean.Compras.Solicitud;
import gui.General.Main;
import java.awt.Component;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author LiziTa
 */
public class SolicitudSeleccionarOrden extends javax.swing.JInternalFrame {
   
TableModel tb= new TableModel();
    /**
     * Creates new form CatalogoSeleccionarProducto
     */
    public SolicitudSeleccionarOrden() {
        initComponents();
         tblOrden.setModel(tb);
         initComp();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
     private PanelSolOrdenAlm panelSol;
       ArrayList<Solicitud> orden=(ArrayList<Solicitud>)Controlador.Service.Compras.GestionCompra.obtenerOrdenMov2();
    Producto p=new Producto();
private PanelSolOrdenAlm solordalm;
    SolicitudSeleccionarOrden(PanelSolOrdenAlm aThis) {
       solordalm=aThis;
      initComponents();
         tblOrden.setModel(tb);
         initComp();
//          TableRowSorter<TableModel> sorter  = new TableRowSorter<TableModel>((TableModel) tblOrden.getModel());
//         tblOrden.setRowSorter(sorter);
         
    }

    private void initComp() {
                tb = new TableModel() ;
     tblOrden.setModel(tb);
tblOrden.setRowSelectionAllowed(true);

        tblOrden.setDefaultRenderer(JButton.class, new SolicitudSeleccionarOrden.MiRender());
        tblOrden.setDefaultEditor( JButton.class, new SolicitudSeleccionarOrden.MiEditor());

        
    }
    
     private static class MiRender implements TableCellRenderer {

       

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            return (JComponent)table.getValueAt(row, column); //To change body of generated methods, choose Tools | Templates.
        }
    }

    private static class MiEditor implements TableCellEditor {

        public MiEditor() {
        }
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }

        @Override
        public boolean isCellEditable(EventObject anEvent) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean shouldSelectCell(EventObject anEvent) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean stopCellEditing() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void cancelCellEditing() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void addCellEditorListener(CellEditorListener l) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void removeCellEditorListener(CellEditorListener l) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
    }

    
    class TableModel extends AbstractTableModel{
        
      String[] columnNames = {"IdOrden","Fecha", "Monto Total","Detalle"};
           
                        
        @Override
        public int getRowCount()
        {
         
            return orden.size();
        }
        
        
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   // {"Razon Social", "Direccion", "Ruc" , "Contacto", "Apellido","Eliminar"};
            Solicitud s= orden.get(row);
            switch(column){
                case 0:return s.getIdOrdenCompra();
                case 1: return s.getFecha();
                case 2: return s.getTotal();    
                default: return new JButton("Detalle") ; 
           
               }
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
         @Override
        public Class getColumnClass(int c) {
            if(c!=3){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
	        return false;
        }

        
        }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblOrden = new javax.swing.JTable();

        setClosable(true);
        setTitle("Seleccionar Orden de Compra");

        tblOrden.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblOrden.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblOrdenMouseClicked(evt);
            }
        });
        tblOrden.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                tblOrdenComponentHidden(evt);
            }
        });
        jScrollPane1.setViewportView(tblOrden);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblOrdenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblOrdenMouseClicked
        // TODO add your handling code here:
       int selectedColumnIndex = tblOrden.getSelectedColumn();
         int selectedRowIndex = tblOrden.getSelectedRow();
         
        if(evt.getClickCount()==2){
            
            if(selectedColumnIndex == 3){
             BuscarSolicitud dt = new  BuscarSolicitud((String)tb.getValueAt(selectedRowIndex, 0));
             Main.getDesktop().add(dt);
            dt.setVisible(true); 
            } else {
            
            int rowSel = tblOrden.getSelectedRow();
            Solicitud pr=orden.get(rowSel);
            solordalm.txtOrden.setText(pr.getIdOrdenCompra().toString());
            String idord=pr.getIdOrdenCompra().toString();
                      
            solordalm.actualizarTable(idord);
            
            SolicitudSeleccionarOrden.this.dispose();
            }
            
        }
         
        
         
           
        
    }//GEN-LAST:event_tblOrdenMouseClicked

    private void tblOrdenComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_tblOrdenComponentHidden
        // TODO add your handling code here:
    }//GEN-LAST:event_tblOrdenComponentHidden

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblOrden;
    // End of variables declaration//GEN-END:variables
}
